# views.py in 'alerts' app
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import alert
from .serializers import AlertSerializer

class AlertHistory(APIView):
    def get(self, request, *args, **kwargs):
        # 可选：可以添加过滤条件，比如开始时间和结束时间
        alerts = alert.objects.all()
        serializer = AlertSerializer(alerts, many=True)
        return Response(serializer.data)

class AlertDetail(APIView):
    def get(self, request, alert_id, *args, **kwargs):
        try:
            # 修改为 alert_instance 避免命名冲突
            alert_instance = alert.objects.get(alert_id=alert_id)
            serializer = AlertSerializer(alert_instance)
            return Response(serializer.data)
        except alert.DoesNotExist:
            return Response({'error': 'Alert not found'}, status=status.HTTP_404_NOT_FOUND)
